{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:46.450657Z",
     "start_time": "2025-03-02T09:29:46.447775Z"
    }
   },
   "source": [
    "print(\"Numpy(Numerical Python)是一个开源的Python科学计算库，用于快速处理任意维度的数组。\")\n",
    "print(\"Numpy支持常见的数组和矩阵操作。对于同样的数值计算任务，使用Numpy比直接使用Python要简洁许多。\")\n",
    "print(\"Numpy使用ndarray对象来处理多维数组，该对象是一个快速而灵活的大数据容器。\")\n",
    "print(\"Numpy里边的数据结构只有一个，即ndarray,ndarray也是一个可变数据类型\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numpy(Numerical Python)是一个开源的Python科学计算库，用于快速处理任意维度的数组。\n",
      "Numpy支持常见的数组和矩阵操作。对于同样的数值计算任务，使用Numpy比直接使用Python要简洁许多。\n",
      "Numpy使用ndarray对象来处理多维数组，该对象是一个快速而灵活的大数据容器。\n",
      "Numpy里边的数据结构只有一个，即ndarray\n"
     ]
    }
   ],
   "execution_count": 165
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:46.640711Z",
     "start_time": "2025-03-02T09:29:46.638631Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import time\n",
    "import random"
   ],
   "id": "6cec8fcb19470643",
   "outputs": [],
   "execution_count": 166
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.849691Z",
     "start_time": "2025-03-02T09:29:46.645024Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())  # random.random() 是 Python 标准库 random 模块中的一个函数，用于生成一个范围在 [0.0, 1.0) 之间的随机浮点数。\n",
    "# time.time() 是 Python 标准库 time 模块中的一个函数，用于获取当前的时间戳。\n",
    "print('-' * 50)\n",
    "t1 = time.time()  # 记录开始时间\n",
    "sum1 = sum(a)  # 直接使用 Python 的 sum() 函数求和\n",
    "t2 = time.time()  # 记录结束时间\n",
    "\n",
    "b = np.array(a)  # 将列表 a 转换为 Numpy 数组\n",
    "t4 = time.time()  # 记录结束时间\n",
    "sum2 = np.sum(b)  # 使用 Numpy 的 sum() 函数求和\n",
    "t5 = time.time()  # 记录结束时间\n",
    "\n",
    "print(\"直接使用 Python 的 sum() 函数耗时：\", t2 - t1, \"秒\")\n",
    "print(\"使用 Numpy 的 sum() 函数耗时：\", t5 - t4, \"秒\")"
   ],
   "id": "5a79f3e443df4f25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "直接使用 Python 的 sum() 函数耗时： 0.5312612056732178 秒\n",
      "使用 Numpy 的 sum() 函数耗时： 0.06899595260620117 秒\n"
     ]
    }
   ],
   "execution_count": 167
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 创建一维数组",
   "id": "363790f478937c0b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.855182Z",
     "start_time": "2025-03-02T09:29:56.851418Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1, 2, 3, 4]\n",
    "oneArray = np.array(list1)\n",
    "print(list1)  # 列表输出的结果是有逗号的\n",
    "print(type(oneArray))  # ndarray的输出结果是不带逗号的\n",
    "print(oneArray)\n",
    "\n",
    "#创建数组的多种形式\n",
    "#1.直接传入列表的方式\n",
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(type(t1))\n",
    "\n",
    "#2.传入range生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))\n",
    "\n",
    "#3.使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0, 10, 2)\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "id": "3dfff143068e76f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 168
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 创建二维数组",
   "id": "45e007ee9e6dc28b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.859006Z",
     "start_time": "2025-03-02T09:29:56.856284Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)"
   ],
   "id": "49730979b2fa849c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 169
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 常用属性：",
   "id": "fcb79b3912e0452c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.862252Z",
     "start_time": "2025-03-02T09:29:56.860067Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "twoArray = np.array(list2)\n",
    "#获取数组的维度(注意：与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "#形状（行，列）\n",
    "print(twoArray.shape)\n",
    "#有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "eec291703c2fe6d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 170
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 调整数组的形状：",
   "id": "87857d5205f826f5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.865642Z",
     "start_time": "2025-03-02T09:29:56.863170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1, 2, 3], [4, 5, 6]])  # 两行三列的数组\n",
    "print(four)\n",
    "\n",
    "#修改的是原有的\n",
    "print(id(four))\n",
    "four.shape = (3, 2)  # 调整为三行两列\n",
    "print(four)\n",
    "print(id(four))"
   ],
   "id": "caf9275af450581b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2057994328400\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2057994328400\n"
     ]
    }
   ],
   "execution_count": 171
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.868679Z",
     "start_time": "2025-03-02T09:29:56.866322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1, 2, 3], [4, 5, 6]])  # 两行三列的数组\n",
    "print(four)\n",
    "#返回一个新的数组\n",
    "print(id(four))\n",
    "four = four.reshape(3, 2)\n",
    "print(four)\n",
    "print(id(four))\n"
   ],
   "id": "390f428655ed8d2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2057994335216\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2057994323312\n"
     ]
    }
   ],
   "execution_count": 172
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.871997Z",
     "start_time": "2025-03-02T09:29:56.869430Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将多维变成一维数组\n",
    "five_c = four.reshape((6,), order='C')  # order也可以不写,如果不指定 order 参数，reshape() 方法默认使用 'C' 风格（行优先）来排列元素。\n",
    "# (6,) 这代表的是一维数组，它只存在一个维度，数组里有 6 个元素。\n",
    "# 但(1,6)代表的还是一个二维数组\n",
    "five_f = four.reshape((6,), order='F')  # 了解即可\n",
    "#默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "print(five_c)\n",
    "print(five_f)\n",
    "# 另外一种展平数组的方法\n",
    "six = four.flatten()  # 主要功能是将多维数组转换为一维数组。它会按照数组在内存中的存储顺序（默认是 C 风格，即行优先）将所有元素依次取出，组合成一个一维数组。\n",
    "print(six)\n"
   ],
   "id": "53d042fa08cf203",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 3 5 2 4 6]\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 173
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:56.876054Z",
     "start_time": "2025-03-02T09:29:56.872763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)  # 输出数组的元素\n",
    "print(t.shape)  # 输出数组的形状，即(24，)\n",
    "#转换成二维\n",
    "t1 = t.reshape((4, 6))  # 把数组的形状改成(4,6)\n",
    "print(t1)  # 输出数组的元素\n",
    "print(t1.shape)  # 输出数组的形状，即(4,6)\n",
    "t2 = t1.reshape((24,))  # 把数组的形状改成(24,)\n",
    "print(t2)  # 输出数组的元素"
   ],
   "id": "ea5e72db34360b6c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n"
     ]
    }
   ],
   "execution_count": 174
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 将数组转换为列表",
   "id": "562acf8133e5d737"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.538084Z",
     "start_time": "2025-03-02T09:29:56.876720Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组转换为列表\n",
    "a = np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist()\n",
    "list_a"
   ],
   "id": "99611465f96fc7cb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 12, 88, 14, 25]"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 175
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### Numpy的数据类型",
   "id": "4a48f46ec00af180"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.542178Z",
     "start_time": "2025-03-02T09:29:57.539437Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f = np.array([1, 2, 3, 4, 5], dtype=np.int16)  # 创建一个int16类型的数组\n",
    "print(f.itemsize)  # np.int8(一个字节) 这里会输出2，即int16类型占两个字节\n",
    "#获取数据类型\n",
    "print(f.dtype)  # 输出int16"
   ],
   "id": "cab5be772f9d39c9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 176
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.549127Z",
     "start_time": "2025-03-02T09:29:57.543100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "\n",
    "#拓展随机生成小数\n",
    "#使用python语法，保留两位\n",
    "print(round(random.random(), 2))\n",
    "\"\"\"\n",
    "round(number, ndigits=None)\n",
    "number：这是必须要提供的参数，它代表需要进行四舍五入操作的数值。这个数值可以是整数，也可以是浮点数。\n",
    "ndigits：这是一个可选参数，它用于指定保留的小数位数。如果省略该参数，或者将其设置为 None，则函数会将 number 四舍五入为最接近的整数。如果 ndigits 是一个正整数，那么 number 会被四舍五入到指定的小数位数；如果 ndigits 是一个负整数，number 会在小数点左边进行四舍五入。\n",
    "\"\"\"\n",
    "arr = np.array([random.random() for i in range(10)], dtype=np.float16)\n",
    "print(arr)\n",
    "#取小数点后两位\n",
    "print(np.round(arr, 2))\n",
    "print(arr.dtype)"
   ],
   "id": "e9bbe64d87561d7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "0.84\n",
      "[0.1875  0.4045  0.544   0.559   0.095   0.255   0.727   0.1816  0.03607\n",
      " 0.573  ]\n",
      "[0.19 0.4  0.54 0.56 0.1  0.25 0.73 0.18 0.04 0.57]\n",
      "float16\n"
     ]
    }
   ],
   "execution_count": 177
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的运算",
   "id": "dcefabdb80ced7b0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.552957Z",
     "start_time": "2025-03-02T09:29:57.549904Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组与数的计算\n",
    "# 由于Numpy的广播机制，可以对不同形状的数组进行运算。在运算过程中，加减乘除的值被广播到所有元素上进行运算。\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(t1)\n",
    "print('-' * 30)\n",
    "print(t1 + 2)\n",
    "print('-' * 30)\n",
    "print(t1 * 2)\n",
    "print('-' * 30)\n",
    "print(t1 / 2)\n",
    "# 这些运算的结果都不会改变原数组的值，而是返回一个新的数组。"
   ],
   "id": "45b7f41a453aab3b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "------------------------------\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "------------------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "------------------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 178
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.556316Z",
     "start_time": "2025-03-02T09:29:57.553741Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组与数组的计算\n",
    "# 同种形状的数组才能进行运算，否则会报错。\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1 + t2)\n",
    "print(t1 - t2)"
   ],
   "id": "797fd6ea00462957",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]]\n"
     ]
    }
   ],
   "execution_count": 179
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.559542Z",
     "start_time": "2025-03-02T09:29:57.557457Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不种形状的多维数组不能计算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(18).reshape((3, 6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "# print(t1 - t2)"
   ],
   "id": "466d2c5a616ab010",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n"
     ]
    }
   ],
   "execution_count": 180
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.563543Z",
     "start_time": "2025-03-02T09:29:57.560500Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的尺寸不一样，其中一个数组的某一个维度长度为1，则会广播到其他维度上。\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "print(t1)\n",
    "t2 = np.arange(0, 6)  # 一维数组\n",
    "print(t2)\n",
    "print(t1 - t2)  # 行形状相同（会与每一行数组的对应位相操作)"
   ],
   "id": "5c50c9cfb5b1d774",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[0 1 2 3 4 5]\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 181
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.566984Z",
     "start_time": "2025-03-02T09:29:57.564229Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4, 6))\n",
    "print(t1)\n",
    "t2 = np.arange(4).reshape((4, 1))\n",
    "print(t2)\n",
    "print(t1 - t2)  # 列形状相同（会与每一列数组的对应位相操作)"
   ],
   "id": "6a7503876023e941",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 182
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组中的轴",
   "id": "4d8385c05b25f9d4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.570211Z",
     "start_time": "2025-03-02T09:29:57.567820Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "1. 什么是轴：在numpy中可以理解为方向，使用0，1，2数字表示，对于一个一维数组，只有一个0轴， 对于2维数\n",
    "组（shape（2，2))有0轴和1轴， 对于3维数组（shape（2，2，3））有0，1，2轴\n",
    "2. 为什么要学习轴：有了轴的概念后，我们计算会更加方便，比如计算一个2维数组的平均值，必须指定是计算哪个\n",
    "方向上面的数字的平均值。\n",
    "\"\"\"\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])  # 按哪个轴计算，哪个轴就会消失\n",
    "print(np.sum(a, axis=0))  # [5 7 9]\n",
    "print(np.sum(a, axis=1))  # [ 6 22]\n",
    "print(np.sum(a))"
   ],
   "id": "853f5e23f9fdc89b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 183
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.574359Z",
     "start_time": "2025-03-02T09:29:57.571091Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24)\n",
    "print(t.shape)\n",
    "a = t.reshape((2, 3, 4))\n",
    "a  # 最外面的方括号是0轴，最内层的方括号是最大轴"
   ],
   "id": "5fb83200ba40b3dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 184
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.613925Z",
     "start_time": "2025-03-02T09:29:57.575004Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.sum(a, axis=0)\n",
    "print(\"-\" * 20)\n",
    "print(f\"b的shape：{b.shape}\")  # (3, 4)\n",
    "c = np.sum(a, axis=1)\n",
    "print(\"-\" * 20)\n",
    "print(f\"c的shape：{c.shape}\")  # (2, 4)\n",
    "d = np.sum(a, axis=2)\n",
    "print(\"-\" * 20)\n",
    "print(f\"d的shape：{d.shape}\")  # (2, 3)"
   ],
   "id": "2d8ff7248b413872",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "b的shape：(3, 4)\n",
      "--------------------\n",
      "c的shape：(2, 4)\n",
      "--------------------\n",
      "d的shape：(2, 3)\n"
     ]
    }
   ],
   "execution_count": 185
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的索引和切片",
   "id": "259c8c218f57f27"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.617927Z",
     "start_time": "2025-03-02T09:29:57.615116Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)  # [0 1 2 3 4 5 6 7 8 9]\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2], a)\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "print(a[2:4])  # 左闭右开"
   ],
   "id": "ba147bc3ad74d0fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 3]\n"
     ]
    }
   ],
   "execution_count": 186
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.623832Z",
     "start_time": "2025-03-02T09:29:57.618770Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t1}\")\n",
    "print()\n",
    "print(f\"以t1[0]的方法取出第一行：\\n{t1[0]}\")  # 取第一行print(f\"取出第二行：\\n{t1[1]}\") # 取一行(一行代表是一条数据，索引也是从0开始的)print(t1[1,:]) \n",
    "\"\"\"\n",
    "在 NumPy 的二维数组里，当你使用 t1[1] 时，其实是在告诉 NumPy 从数组的行维度中选取索引为 1 的那一行。由于这里没有指定列的信息，NumPy 会默认选取这一行的所有列元素，最终得到的就是数组的第二行。\n",
    "t1[1, :]这里使用了两个索引值，中间用逗号分隔。逗号前面的 1 表示在行维度上选取索引为 1 的行，也就是第二行；逗号后面的 : 是切片操作符，它代表在列维度上选取所有的列。所以 t1[1, :] 同样是获取数组的第二行。\n",
    "\"\"\"\n",
    "print()\n",
    "print(f\"以t1[1:]的方法取出后三行：\\n{t1[1:]}\")  # 取连续的多行\n",
    "\"\"\"\n",
    "t1[1:] 是对二维数组 t1 进行的行切片操作。在 NumPy 中，当我们对多维数组进行索引或切片时，逗号用于分隔不同的维度。对于二维数组，逗号前面的部分用于操作行，逗号后面的部分用于操作列。\n",
    "\"\"\"\n",
    "print()\n",
    "print(f\"以t1[1:3,:]的方法取出第二行到第三行：\\n{t1[1:3, :]}\")  # 取连续的多行\n",
    "print()\n",
    "print(f\"以t1[[0,2,3]]的方法取出第一行、第三行、第四行：\\n{t1[[0, 2, 3]]}\")  # 取不连续的多行\n",
    "\"\"\"\n",
    "t1[[0, 2, 3]]：使用高级索引取不连续的多行\n",
    "当需要选取不连续的行时，就不能简单地用切片操作了，而是要使用高级索引。\n",
    "\"\"\"\n",
    "print()\n",
    "print(f\"以t1[[0,2,3],:]的方法取出第一行、第三行、第四行：\\n{t1[[0, 2, 3], :]}\")  # 取不连续的多行\n",
    "print()\n",
    "print(f\"以t1[:,1]的方法取出第二列：\\n{t1[:, 1]}\")  # 取一列\n",
    "print()\n",
    "print(f\"以t1[:,1:]的方法取出第二列到最后一列：\\n{t1[:, 1:]}\")  # 连续的多列\n",
    "print()\n",
    "print(f\"以t1[:,[0,2,3]]的方法取出第一列、第三列、第四列：\\n{t1[:, [0, 2, 3]]}\")  # 取不连续的多列\n",
    "print()\n",
    "print(f\"以t1[2,3]的方法取出第三行第四列的值：\\n{t1[2, 3]}\")  # 取某一个值,三行四列\n",
    "print()\n",
    "print(f\"以t1[2:4,3:5]的方法取出第三行到第四行，第四列到第六列：\\n{t1[2:4, 3:5]}\")  # 取某一块区域\n",
    "print()\n",
    "print(f\"以t1[[1,2],[0,2]]的方法取出第二行第一列和第三行第三列：\\n{t1[[1, 2], [0, 2]]}\")"
   ],
   "id": "bcb05e2a8b5aebbf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "以t1[0]的方法取出第一行：\n",
      "[0 1 2 3 4 5]\n",
      "\n",
      "以t1[1:]的方法取出后三行：\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "以t1[1:3,:]的方法取出第二行到第三行：\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "\n",
      "以t1[[0,2,3]]的方法取出第一行、第三行、第四行：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "以t1[[0,2,3],:]的方法取出第一行、第三行、第四行：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "以t1[:,1]的方法取出第二列：\n",
      "[ 1  7 13 19]\n",
      "\n",
      "以t1[:,1:]的方法取出第二列到最后一列：\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "\n",
      "以t1[:,[0,2,3]]的方法取出第一列、第三列、第四列：\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "\n",
      "以t1[2,3]的方法取出第三行第四列的值：\n",
      "15\n",
      "\n",
      "以t1[2:4,3:5]的方法取出第三行到第四行，第四列到第六列：\n",
      "[[15 16]\n",
      " [21 22]]\n",
      "\n",
      "以t1[[1,2],[0,2]]的方法取出第二行第一列和第三行第三列：\n",
      "[ 6 14]\n"
     ]
    }
   ],
   "execution_count": 187
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的修改：",
   "id": "400a67d113cd0b4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.627020Z",
     "start_time": "2025-03-02T09:29:57.624440Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改某一行的值\n",
    "t[1, :] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "a20481558d081d0a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 188
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.630484Z",
     "start_time": "2025-03-02T09:29:57.627858Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "9cf5f5fa9fe950cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  0  2  3  4  5]\n",
      " [ 6  0  8  9 10 11]\n",
      " [12  0 14 15 16 17]\n",
      " [18  0 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 189
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.633732Z",
     "start_time": "2025-03-02T09:29:57.631283Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "608fa402b2581005",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 190
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.637010Z",
     "start_time": "2025-03-02T09:29:57.634546Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "ee2eca515f8c5c47",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 6  0  0  0 10 11]\n",
      " [12  0  0  0 16 17]\n",
      " [18  0  0  0 22 23]]\n"
     ]
    }
   ],
   "execution_count": 191
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.640182Z",
     "start_time": "2025-03-02T09:29:57.637781Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改某一块区域的值\n",
    "t[1:4, 2:5] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "b6af2612d2944344",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  0  0  0 11]\n",
      " [12 13  0  0  0 17]\n",
      " [18 19  0  0  0 23]]\n"
     ]
    }
   ],
   "execution_count": 192
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.643192Z",
     "start_time": "2025-03-02T09:29:57.641077Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 修改多个不相邻的点\n",
    "t[[0, 1], [0, 3]] = 0  # 将数组 t 中位于 (0, 0) 和 (1, 3) 位置的元素修改为 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "36a36c4692b204f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  0 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 193
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.648346Z",
     "start_time": "2025-03-02T09:29:57.645206Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 可以根据条件修改，比如将数组中小于10的值改掉\n",
    "t[t < 10] = 0\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "ae68753bd6dce7c2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 194
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.651752Z",
     "start_time": "2025-03-02T09:29:57.649059Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 使用逻辑判断\n",
    "t[(t > 2) & (t < 6)] = 0  # 与\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "3bbe9a96373fa68a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  1  2  0  0  0]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 195
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.654829Z",
     "start_time": "2025-03-02T09:29:57.652588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 使用逻辑判断\n",
    "t[(t < 2) | (t > 6)] = 0  # 或\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "ff936058bed59dcc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[0 0 2 3 4 5]\n",
      " [6 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n"
     ]
    }
   ],
   "execution_count": 196
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.658836Z",
     "start_time": "2025-03-02T09:29:57.655763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "# 使用逻辑判断\n",
    "t[~(t > 6)] = 0  # 非\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "3798df8c422cad67",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 197
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.662215Z",
     "start_time": "2025-03-02T09:29:57.659577Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(t < 10)\n",
    "print(t[t < 10])"
   ],
   "id": "bc9ab3263f0290e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True  True  True  True  True  True]\n",
      " [ True  True  True  True False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 198
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.666245Z",
     "start_time": "2025-03-02T09:29:57.663047Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "print(f\"原始数组：\\n{t}\")\n",
    "t = t.clip(10, 18)  # 将数组中的值限制在10到18之间\n",
    "print(f\"修改后数组：\\n{t}\")"
   ],
   "id": "846987a3a4553888",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "修改后数组：\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 199
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.669761Z",
     "start_time": "2025-03-02T09:29:57.667173Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓 展\n",
    "# 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(score)\n",
    "result = np.where(score > 80, True, False)\n",
    "print(result)"
   ],
   "id": "641f95fce7f27ddd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 200
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组的添加，删除，去重：",
   "id": "b16c1feebf3067c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.673739Z",
     "start_time": "2025-03-02T09:29:57.670502Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(f'原始数组a：\\n{a}')\n",
    "print()\n",
    "print(\"向数组添加元素：\")\n",
    "print(\"(如果不指定轴，则a会被展开，然后再添加元素)\")\n",
    "print(np.append(a, [7, 8, 9]))  # np.append(arr, values, axis=None) 用于在数组的末尾追加值，返回一个新的数组。\n",
    "print()\n",
    "print(\"沿着轴0添加元素：\")\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print()\n",
    "print(\"沿着轴1添加元素：\")\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))"
   ],
   "id": "9d976a9052528f88",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组a：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "向数组添加元素：\n",
      "(如果不指定轴，则a会被展开，然后再添加元素)\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "沿着轴0添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "沿着轴1添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 201
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.678799Z",
     "start_time": "2025-03-02T09:29:57.674621Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "np.insert(arr, obj, values, axis=None)\n",
    "arr（必须）:表示要进行插入操作的原始数组，可以是一维、二维或更高维度的 NumPy 数组。\n",
    "obj（必须）:指定插入值的位置。它可以是一个整数、整数数组或者切片对象。\n",
    "          当 axis 为 None 时，arr 会被展平为一维数组，obj 表示在展平后的数组中的索引位置。\n",
    "          当指定了 axis 时，obj 表示在指定轴上的索引位置。\n",
    "values（必须）:要插入到 arr 中的值。values 可以是单个值、列表、元组或者另一个 NumPy 数组。它的形状需要和 arr 在插入操作时的形状相匹配。\n",
    "axis（可选）:指定在哪个轴上进行插入操作。默认值为 None。\n",
    "           axis=None：当 axis 为 None 时，arr 会先被展平为一维数组，然后在展平后的数组的指定位置插入 values，最终返回一个一维数组。\n",
    "           axis=0：在二维数组中，axis = 0 表示按行插入，即垂直方向。此时要求插入的 values 的列数与 arr 的列数相同。\n",
    "           axis=1：在二维数组中，axis = 1 表示按列插入，即水平方向。此时要求插入的 values 的行数与 arr 的行数相同。\n",
    "\"\"\"\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print(f'原始数组：\\n{a}')\n",
    "print()\n",
    "print(\"插入数组：\")\n",
    "print('（未传递 Axis 参数。 在插入之前输入数组会被展开）')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('（传递了 Axis 参数。 会广播值数组来配输入数组）')\n",
    "print('沿轴 0 广播：')\n",
    "print(np.insert(a, 1, [11], axis=0))  # 广播：我们只写了一个元素，当被广播成了多个\n",
    "print('沿轴 1 广播：')\n",
    "print(np.insert(a, 1, 11, axis=1))  # 当只有一个值的时候，写不写[]都没有影响\n",
    "print()\n",
    "print(\"不广播，写多个元素也是可以的：\")\n",
    "print(np.insert(a, 1, [11, 12], axis=0))"
   ],
   "id": "45d5081d3d8b3130",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "插入数组：\n",
      "（未传递 Axis 参数。 在插入之前输入数组会被展开）\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "（传递了 Axis 参数。 会广播值数组来配输入数组）\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "沿轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n",
      "\n",
      "不广播，写多个元素也是可以的：\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n"
     ]
    }
   ],
   "execution_count": 202
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.682793Z",
     "start_time": "2025-03-02T09:29:57.679626Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3, 4)\n",
    "print(f'原始数组：\\n{a}')\n",
    "print()\n",
    "print('（未传递 Axis 参数。 在删除之前输入数组会被展开）')\n",
    "print(np.delete(a, 5))\n",
    "print()\n",
    "print('（传递了 Axis 参数。 会删除指定轴上的元素）')\n",
    "print('删除每一行中的第二列：')\n",
    "print(np.delete(a, 1, axis=1))\n",
    "print()\n",
    "print('删除每一列中的第二行：')\n",
    "print(np.delete(a, 1, axis=0))"
   ],
   "id": "7c763b30537b0de0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "（未传递 Axis 参数。 在删除之前输入数组会被展开）\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "（传递了 Axis 参数。 会删除指定轴上的元素）\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "删除每一列中的第二行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "execution_count": 203
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.686877Z",
     "start_time": "2025-03-02T09:29:57.683558Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 去重\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])\n",
    "print(f'原始数组：\\n{a}')\n",
    "print()\n",
    "print(\"原始数组去重：\")\n",
    "print(np.unique(a))  # 会返回一个新的数组，不改变原数组"
   ],
   "id": "efae6a61393701fe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "原始数组去重：\n",
      "[2 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 204
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.691131Z",
     "start_time": "2025-03-02T09:29:57.687609Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的计算\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(f'原始数组：\\n{score}')\n",
    "print()\n",
    "# 1. 获取所有数据的最大值\n",
    "result = np.max(score)  # 依旧满足对谁求，谁就消失\n",
    "print(f'获取所有数据的最大值：\\n{result}')\n",
    "print()\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score, axis=0)\n",
    "# 在3行2列的数组中沿着轴0（行）的方向求最大值，沿着哪个轴操作哪个轴就要消失，所有3行消失，剩下2列\n",
    "print(f'获取某一轴上的数据最大值：\\n{result}')\n",
    "print()\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(f'获取所有数据的最小值：\\n{result}')\n",
    "print()\n",
    "# 4. 获取某一轴上的数据最小值\n",
    "result = np.min(score, axis=1)\n",
    "print(f'获取某一轴上的数据最小值：\\n{result}')\n",
    "print()\n",
    "# 5. 数据的比较\n",
    "result = np.maximum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(f'数据比较：\\n{result}')\n",
    "print()\n",
    "result = np.minimum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(f'数据比较：\\n{result}')\n",
    "print()\n",
    "result = np.maximum([5, -1, 0, 1, 2], [1, 2, 3, 4, 5])\n",
    "print(f'数据比较：\\n{result}')\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制\n",
    "print()"
   ],
   "id": "78cce5dbe7b065bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "\n",
      "获取所有数据的最大值：\n",
      "88\n",
      "\n",
      "获取某一轴上的数据最大值：\n",
      "[82 88]\n",
      "\n",
      "获取所有数据的最小值：\n",
      "75\n",
      "\n",
      "获取某一轴上的数据最小值：\n",
      "[80 81 75]\n",
      "\n",
      "数据比较：\n",
      "[0 0 0 1 2]\n",
      "\n",
      "数据比较：\n",
      "[-2 -1  0  0  0]\n",
      "\n",
      "数据比较：\n",
      "[5 2 3 4 5]\n",
      "\n"
     ]
    }
   ],
   "execution_count": 205
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.694124Z",
     "start_time": "2025-03-02T09:29:57.691726Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 6. 求平均值\n",
    "result = np.mean(score)  # 获取所有数据的平均值\n",
    "print(f'获取所有数据的平均值：\\n{result}')\n",
    "print()\n",
    "result = np.mean(score, axis=0)  # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "print()"
   ],
   "id": "cac59aa82fa2c39",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取所有数据的平均值：\n",
      "81.16666666666667\n",
      "\n",
      "[79.         83.33333333]\n",
      "\n"
     ]
    }
   ],
   "execution_count": 206
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.697563Z",
     "start_time": "2025-03-02T09:29:57.694816Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 7. 求前缀和\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(f'原始数组：\\n{arr}')\n",
    "print()\n",
    "print(f\"按照轴0的求前缀和：\\n{arr.cumsum(0)}\")\n",
    "print()\n",
    "print(f\"按照轴1的求前缀和：\\n{arr.cumsum(1)}\")"
   ],
   "id": "e0205aa5afbac559",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "按照轴0的求前缀和：\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "\n",
      "按照轴1的求前缀和：\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 207
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.700794Z",
     "start_time": "2025-03-02T09:29:57.698343Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 8. argmin 求最小值索引\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(f'原始数组：\\n{score}')\n",
    "print()\n",
    "result = np.argmin(score, axis=0)\n",
    "res = np.min(score, axis=0)  #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "print(result, res)"
   ],
   "id": "7668189ff2877eba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组：\n",
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "\n",
      "[2 1] [75 81]\n"
     ]
    }
   ],
   "execution_count": 208
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.703823Z",
     "start_time": "2025-03-02T09:29:57.701526Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score, axis=0)\n",
    "print(f'按照轴0求的标准差：\\n{result}')"
   ],
   "id": "28c70ad7f3e5dc39",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按照轴0求的标准差：\n",
      "[2.94392029 3.29983165]\n"
     ]
    }
   ],
   "execution_count": 209
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.706835Z",
     "start_time": "2025-03-02T09:29:57.704604Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 10. 极 值\n",
    "result = np.ptp(score, axis=None)  # 就是最大值和最小值的差\n",
    "print(result)"
   ],
   "id": "a779d3cd679ceea1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "execution_count": 210
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.709975Z",
     "start_time": "2025-03-02T09:29:57.707541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.log2(10))\n",
    "print(np.log(10))  # ln(x)\n",
    "print(np.log10(10))"
   ],
   "id": "5e6157f43eb67136",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.321928094887362\n",
      "2.302585092994046\n",
      "1.0\n"
     ]
    }
   ],
   "execution_count": 211
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 拼接与分割",
   "id": "8246528691ea4b18"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:29:57.727244Z",
     "start_time": "2025-03-02T09:29:57.710637Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 11.拼接与分割\n",
    "# 有的时候我们需要将两个数据加起来一起研究分析，我们就可以将其进行拼接然后分析\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "print(\"沿着轴0拼接两个数组：\")\n",
    "c = np.concatenate((a, b), axis=0)\n",
    "print(c, c.shape)\n",
    "print()\n",
    "print(\"沿着轴1拼接两个数组：\")\n",
    "d = np.concatenate((a, b), axis=1)\n",
    "print(d, d.shape)"
   ],
   "id": "61089fe208fb20c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿着轴0拼接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]] (4, 2)\n",
      "\n",
      "沿着轴1拼接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]] (2, 4)\n"
     ]
    }
   ],
   "execution_count": 212
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:37:23.510169Z",
     "start_time": "2025-03-02T09:37:23.506323Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用stack拼接会增加维度\n",
    "a = np.arange(12).reshape(3,4)\n",
    "b = np.arange(12).reshape(3,4)\n",
    "print('沿着轴0拼接两个数组：')\n",
    "a_1 = np.stack((a,b),axis=0)\n",
    "print(a_1,'\\n',a_1.shape)\n",
    "print()\n",
    "print('沿着轴1拼接两个数组：')\n",
    "b_1 = np.stack((a,b),axis=1)\n",
    "print(b_1,'\\n',b_1.shape)\n",
    "print('沿着轴2拼接两个数组：')\n",
    "c_1 = np.stack((a,b),axis=2)\n",
    "print(c_1,'\\n',c_1.shape)"
   ],
   "id": "e7c26bc0e53e94e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿着轴0拼接两个数组：\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]] \n",
      " (2, 3, 4)\n",
      "\n",
      "沿着轴1拼接两个数组：\n",
      "[[[ 0  1  2  3]\n",
      "  [ 0  1  2  3]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [ 8  9 10 11]]] \n",
      " (3, 2, 4)\n",
      "沿着轴2拼接两个数组：\n",
      "[[[ 0  0]\n",
      "  [ 1  1]\n",
      "  [ 2  2]\n",
      "  [ 3  3]]\n",
      "\n",
      " [[ 4  4]\n",
      "  [ 5  5]\n",
      "  [ 6  6]\n",
      "  [ 7  7]]\n",
      "\n",
      " [[ 8  8]\n",
      "  [ 9  9]\n",
      "  [10 10]\n",
      "  [11 11]]] \n",
      " (3, 4, 2)\n"
     ]
    }
   ],
   "execution_count": 217
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T09:56:44.827233Z",
     "start_time": "2025-03-02T09:56:44.823152Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的分割\n",
    "# 将一个数组分割成为多个子数组\n",
    "\"\"\"\n",
    "numpy.split(ary, indices_or_sections, axis=0)\n",
    "ary（必须）：要进行分割的输入数组;类型为numpy.ndarray。\n",
    "indices_or_sections（必须）：该参数指定了分割的方式，有两种不同的使用方式，具体取决于它的数据类型。\n",
    "如果 indices_or_sections 是一个整数 N，则函数会尝试将数组 ary 沿着指定的 axis 平均分割成 N 个子数组。需要注意的是，数组的长度必须能被 N 整除，否则会抛出 ValueError 异常。如果 indices_or_sections 是一个一维数组，数组中的元素表示分割点的索引。函数会在这些索引处将数组 ary 分割成多个子数组。\n",
    "axis（可选）：指定沿哪个轴进行分割，默认为 0。\n",
    "\"\"\"\n",
    "\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "print(f\"原数组为：\\n{arr}\")\n",
    "print('将数组分成3个大小相等的子数组：')\n",
    "b = np.split(arr,3)\n",
    "print(b)\n",
    "print(type(b))\n",
    "print(type(b[0]))\n",
    "c = np.split(arr,3,axis=1)\n",
    "print(c)"
   ],
   "id": "915d6afb07f32ba1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组为：\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "将数组分成3个大小相等的子数组：\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6]]), array([[1],\n",
      "       [4],\n",
      "       [7]]), array([[2],\n",
      "       [5],\n",
      "       [8]])]\n"
     ]
    }
   ],
   "execution_count": 226
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:06:53.546291Z",
     "start_time": "2025-03-02T10:06:53.541191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2.numpy.hsplit 函数用于水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "harr = np.floor(10*np.random.random((2, 6)))  # floor() 函数用于向下取整,ceil() 函数用于向上取整,round() 函数用于四舍五入\n",
    "\"\"\"\n",
    "此代码的功能是生成一个形状为 (2, 6) 的二维数组，数组中的每个元素都是小于 10 的非负整数。\n",
    "np.random.random() 是 NumPy 里用于生成随机浮点数的函数，这些随机数的取值范围是 [0, 1)。\n",
    "(2, 6) 作为参数传递给 np.random.random()，意味着生成一个形状为 (2, 6) 的二维数组，也就是 2 行 6 列的数组。\n",
    "10*np.random.random((2, 6))：将生成的随机数数组中的每个元素都乘以 10，这样数组元素的取值范围就变成了 [0, 10)。\n",
    "np.floor() 是 NumPy 中的向下取整函数，它会把数组中每个元素向下取整为最接近的整数。\n",
    "经过 np.floor() 处理后，数组元素的取值范围变为 [0, 9] 的整数。\n",
    "\n",
    "numpy.random.random(size=None)\n",
    "size：\n",
    "含义：该参数用于指定生成的随机数数组的形状。它可以是一个整数、一个整数元组或者 None。\n",
    "类型：整数、整数元组或者 None\n",
    "详细解释：\n",
    "size 为 None 时：函数将返回一个位于 [0.0, 1.0) 区间内的单个随机浮点数。\n",
    "size 为整数 n 时：函数会返回一个包含 n 个随机浮点数的一维数组，这些随机浮点数的取值范围同样是 [0.0, 1.0)。\n",
    "size 为整数元组 (m, n, ...) 时：函数会返回一个多维数组，其形状由该元组指定，数组中的每个元素都是 [0.0, 1.0) 区间内的随机浮点数。\n",
    "\"\"\"\n",
    "print ('原 array：')\n",
    "print(harr)\n",
    "print ('拆分后：')\n",
    "print(np.hsplit(harr, 3))\n",
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4,4)\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('竖直分割：')\n",
    "b = np.vsplit(a,2)\n",
    "print (b)\n",
    "# 这两个函数都可以被np.split()函数通过换轴的方式来代替。"
   ],
   "id": "5f9f7cffa065ca36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原 array：\n",
      "[[9. 4. 9. 9. 4. 3.]\n",
      " [3. 9. 2. 3. 0. 0.]]\n",
      "拆分后：\n",
      "[array([[9., 4.],\n",
      "       [3., 9.]]), array([[9., 9.],\n",
      "       [2., 3.]]), array([[4., 3.],\n",
      "       [0., 0.]])]\n",
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "竖直分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 228
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 数组中的nan和inf",
   "id": "f91596cb9daf8385"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:10:39.972010Z",
     "start_time": "2025-03-02T10:10:39.968980Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 12.数组中的nan(not a number)和inf(无穷大）\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))"
   ],
   "id": "5d362a046c8c4dfa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 229
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:12:07.345184Z",
     "start_time": "2025-03-02T10:12:07.339167Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan == np.nan  # np.nan和np.nan虽然长得一样，但是它们不是同一个值 ",
   "id": "a5dea4b15e03fda9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 230
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:13:06.831374Z",
     "start_time": "2025-03-02T10:13:06.827425Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf == np.inf  ",
   "id": "a3768b2db891fca8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 231
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:13:42.622828Z",
     "start_time": "2025-03-02T10:13:42.618965Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf >-np.inf  # 正无穷大大于负无穷大",
   "id": "5a72d071fecdc60a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 232
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:25:48.449350Z",
     "start_time": "2025-03-02T10:25:48.444581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "print(t)\n",
    "t[3,4] = np.nan\n",
    "print(t)\n",
    "print(t != t)  # 将只有一个True\n",
    "print(np.count_nonzero(t != t))  # 所有的False都是0，只有True为非0。这样可以判断出一组数据在nan的个数\n",
    "\"\"\"\n",
    "np.isnan() 是 NumPy 库中的一个函数，用于判断数组中的元素是否为 NaN（Not a Number）。\n",
    "numpy.isnan(x)\n",
    "x：\n",
    "含义：输入的数组或标量，可以是任何可以转换为 NumPy 数组的对象，如 Python 列表、元组等。\n",
    "类型：数组类对象或标量\n",
    "返回值\n",
    "返回一个布尔类型的数组，其形状与输入数组 x 相同。如果 x 中的某个元素是 NaN，则返回的数组中对应位置的元素为 True；否则为 False。\n",
    "\"\"\"\n",
    "np.isnan(t)"
   ],
   "id": "1622b73c484f82c2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]]\n",
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False,  True, False]])"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 236
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:27:31.992689Z",
     "start_time": "2025-03-02T10:27:31.989987Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t[np.isnan(t)]=0\n",
    "\"\"\"\n",
    "在 NumPy 中，np.isnan(t) 可以当作数组 t 的下标，这是因为 NumPy 支持布尔索引（Boolean indexing）这种强大的索引机制。\n",
    "布尔索引允许使用一个布尔类型的数组来选择另一个数组中的元素。具体来说，当你使用一个布尔数组作为索引时，NumPy 会选取布尔数组中值为 True 所对应的位置上的元素。\n",
    "\"\"\"\n",
    "print(t)"
   ],
   "id": "bd7b097f24ddaf36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 237
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:38:36.989859Z",
     "start_time": "2025-03-02T10:38:36.985941Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4,6).astype('float') # 在 NumPy 中，astype() 是 ndarray 对象的一个方法，用于将数组的数据类型转换为指定的数据类型。\n",
    "print(t)\n",
    "print()\n",
    "# 将数组中的一部分替换nan\n",
    "t[1,3:] = np.nan\n",
    "print(t)\n",
    "print('-'*50)\n",
    "for i in range(t.shape[1]):  # t.shape返回的是一个元组，内容为：(4,6) 再由[1]取出下标为1的6，即：t.shape[1] = 6\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:,i]\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]  # 防止nan与其他数相加仍为nan的情况发生\n",
    "        # 将nan替换成这一列的平均值\n",
    "        temp_col[np.isnan( temp_col )] = np.mean( temp_col_not_nan )  # 布尔索引"
   ],
   "id": "5de648fa5523e8bd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "--------------------------------------------------\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 240
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 转置与轴滚动",
   "id": "5ffd814efa862a0b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:48:24.155053Z",
     "start_time": "2025-03-02T10:48:24.151565Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 13.转置与轴滚动\n",
    "# 转置：将数组的行列互换\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a))"
   ],
   "id": "8a08f99d2e0d59c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 241
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:49:17.933565Z",
     "start_time": "2025-03-02T10:49:17.930882Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 与transpose 一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('转置数组：')\n",
    "print (a.T)"
   ],
   "id": "55e53e1e1b964aad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 242
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:50:54.649296Z",
     "start_time": "2025-03-02T10:50:54.645862Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print (' 原 数 组 ：')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "print ('调用 swapaxes 函数后的数组：')\n",
    "print (re)"
   ],
   "id": "c89749ac17eef422",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 243
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-02T10:56:03.226744Z",
     "start_time": "2025-03-02T10:56:03.223424Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 轴滚动\n",
    "t = np.ones((3,4,5,6))  \n",
    "\"\"\"\n",
    "np.ones() 是 NumPy 库中的一个函数，用于创建一个指定形状且元素全为 1 的数组。\n",
    "numpy.ones(shape, dtype=None, order='C')\n",
    "\"\"\"\n",
    "t1 = np.rollaxis(t,3,0)  # 把t1的第3轴三轴（即：6）放到t1的第0轴（即：3），然后3，4，5再往后移\n",
    "print(t1.shape)"
   ],
   "id": "ba319243abc9c5de",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 3, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 245
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
